package com.animee.app4.utils;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtils{
    public static String getJsonByNetwork(String urlpath){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            URL url = new URL(urlpath);   //1.获得网址对象
            //2.获取连接对象，打开连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //3.设置浏览器代理
            conn.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //4.获取输入流对象
            InputStream is = conn.getInputStream();
            int len = 0;
            byte[]data = new byte[1024];
            while ((len = is.read(data))!=-1){
                baos.write(data,0,len);
            }
            is.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return baos.toString();
    }
}